$(document).ready(function(){
    fetchMyWorkflowRequests();
})

function fetchMyWorkflowRequests(){
    
    var url = localStorage["cdUrlEcm"];
    var idCompany = localStorage["idCompany"];
    var cdLogin = localStorage["cdLogin"];
    var cdPassword = localStorage["cdPassword"];
    var idUser = localStorage["idUser"];
    
    var async = true;
        
    url += "/DashBoardService";
    
    var params = new SOAPClientParameters();
    
    params.add("username", cdLogin);
    params.add("password", cdPassword);
    params.add("companyId", parseInt(idCompany));
    params.add("colleagueId", idUser);
    
    SOAPClient.invoke(url, "findMyRequests", params, async, reqCallback);
    
}

function reqCallback(obj, xml) {
    
    var tasks = [];
    
    $(xml).find("item").each(function(){
        var taskItem = $(this);
        var task = {
            "instanceId": parseInt(taskItem.find("processInstanceId").text()),
            "processName": taskItem.find("processDescription").text(),
            "currentState": taskItem.find("stateDescription").text(),
            "currentColleague": taskItem.find("colleagueName").text()
        }
        tasks.push(task);
    });
    
    createTable(tasks);
}

function createTable(data) {
    
    var aaData = [];
    var aoColumns = [
        {"sTitle": "Solicitação"},
        {"sTitle": "Processo"},
        {"sTitle": "Atividade Atual"},
        {"sTitle": "Colaborador"}
    ];
    
    $(data).each(function(){
        aaData.push([this.instanceId, this.processName, this.currentState, this.currentColleague]);
    });
    
    $("#tasksContainer").html('<table cellpadding="0" cellspacing="0" border="0" class="display" id="tblTasks"></table>');
    $("#tblTasks").dataTable({
        "aaData": aaData,
        "aoColumns": aoColumns
    })
    
}
    